<?php

use Drupal\shusy_team\Entity\ShusyTeam;

function shusy_team_shusy_team_entity_form($form, &$form_state, ShusyTeam $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_team') . '/shusy_team.forms.inc';
  $form_state[ShusyTeam::class] = $e;

  //等级信息冗余 1 市场高级顾问 2 市场总监 3 市场部长 4 分红型股东
  $form['level'] = [
    '#type' => 'select',
    '#title' => t('身份等级'),
    '#options' => [
      1 => t('市场高级顾问'),
      2 => t('市场总监'),
      3 => t('市场部长'),
      4 => t('分红型股东'),
    ],
    '#default_value' => $e->level,
  ];
  $form['market_1'] = [
    '#type' => 'textfield',
    '#title' => t('市场高级顾问'),
    '#size' => 8,
    '#required' => TRUE,
    '#default_value' => $e->market_1,
  ];
  $form['market_2'] = [
    '#type' => 'textfield',
    '#title' => t('市场总监'),
    '#size' => 8,
    '#required' => TRUE,
    '#default_value' => $e->market_2,
  ];
  $form['company_1'] = [
    '#type' => 'textfield',
    '#title' => t('市场部长'),
    '#size' => 8,
    '#required' => TRUE,
    '#default_value' => $e->company_1,
  ];
  $form['company_2'] = [
    '#type' => 'textfield',
    '#title' => t('分红型股东'),
    '#size' => 8,
    '#required' => TRUE,
    '#default_value' => $e->company_2,
  ];
  $form['company_3'] = [
    '#type' => 'textfield',
    '#title' => t('股东自收益'),
    '#size' => 8,
    '#required' => TRUE,
    '#default_value' => $e->company_3,
  ];

  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('有效'),
    '#default_value' => $e->status,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_team_shusy_team_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyTeam::class];
  if ($e instanceof ShusyTeam) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_team_shusy_team_delete_form($form, &$form_state, ShusyTeam $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_team') . '/shusy_team.forms.inc';
  $form_state[ShusyTeam::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_team/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_team_shusy_team_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyTeam::class];
  if ($e instanceof ShusyTeam) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_team';
}


function shusy_team_clear_form($form, &$form_state) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_team') . '/shusy_team.forms.inc';
  return confirm_form($form,
    t('确认清空所有团队推广分润关系数据?'),
    'admin/content/shusy_team',
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_team_clear_form_submit($form, &$form_state) {
  db_truncate('shusy_team')->execute();
  db_truncate('shusy_team_revision')->execute();
  $form_state['redirect'] = 'admin/content/shusy_team';
}
